package com.lzp.controller;

import com.lzp.pojo.SysUser;
import com.lzp.utils.JSONResult;
import com.lzp.utils.JsonUtils;
import com.lzp.utils.RedisOperator;
import org.n3r.idworker.Sid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * @description: Redis API
 * @author: lzp
 * @createTime: 2021-08-09 12:12
 **/
@RestController
@RequestMapping("redis")
public class RedisController {

    @Autowired
    private Sid sid;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private RedisOperator redisOperator;

    @RequestMapping("test")
    public JSONResult test() {
        redisTemplate.opsForValue().set("lzp", "test");

        SysUser user = new SysUser();
        String id = sid.nextShort();
        user.setId(id);
        user.setPassword("123");
        user.setUsername("redis");
        user.setNickname("redis");
        user.setIsDelete(0);
        user.setRegistTime(new Date());
        redisTemplate.opsForValue().set("redis:user:" + id, JsonUtils.objectToJson(user));

        SysUser redisUser = JsonUtils.jsonToPojo(
                redisTemplate.opsForValue().get("redis:user:" + id),
                SysUser.class);

        return JSONResult.ok(redisUser);
    }
}
